package br.ufmg.ubicomp.droidguide.application;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import br.ufmg.ubicomp.droidguide.R;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConfigurationManager;
import br.ufmg.ubicomp.droidguide.common.DroidGuideConstants;
import br.ufmg.ubicomp.droidguide.utils.AndroidUtils;
import br.ufmg.ubicomp.droidguide.utils.StringUtils;

public class Config extends DroidGuideScreenActivity implements OnClickListener {

	EditText url, devName, updtInterval;
	Spinner notLevel;
	Button ok;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);
		
		loadConfigurations();
		
		ok = (Button) findViewById(R.id.ok);		
		ok.setOnClickListener(this);
		
	}

	private void loadConfigurations() {
		DroidGuideConfigurationManager recStore = DroidGuideConfigurationManager.getInstance();
		
		url = (EditText) findViewById(R.id.urlText);
		url.setText(recStore.getData(DroidGuideConstants.SERVER));

		devName = (EditText) findViewById(R.id.devNameText);
		devName.setText(recStore.getData(DroidGuideConstants.DEVICE_NAME));
		
		updtInterval = (EditText) findViewById(R.id.evtUpdateText);
		updtInterval.setText(recStore.getData(DroidGuideConstants.UPDATE_INTERVAL));
		
		notLevel = (Spinner) findViewById(R.id.notificationLevel);
		notLevel.setAdapter(createAdapter(R.array.NotificationLevel));
		String sNotLevel = recStore.getData(DroidGuideConstants.NOTIFICATION_LEVEL);
		
		String [] values = getResources().getStringArray(R.array.NotificationLevel);
		for (int i = 0; i < values.length; i++)
		{
			if (values[i].equals(sNotLevel))
			{
				notLevel.setSelection(i);
				break;
			}
		}
	}

	@Override
	public void onClick(View arg0) {
		
		if (StringUtils.isEmpty(url.getText().toString()) ||
				StringUtils.isEmpty(devName.getText().toString()) ||
				StringUtils.isEmpty(updtInterval.getText().toString()) ||
				StringUtils.isEmpty(notLevel.getSelectedItem().toString()))
		{
			AndroidUtils.showAlert(this, "Fields cannot be empty.");
		} else
		{
			DroidGuideConfigurationManager recStore = DroidGuideConfigurationManager.getInstance();
			
			recStore.addData(DroidGuideConstants.SERVER, url.getText().toString());
			recStore.addData(DroidGuideConstants.DEVICE_NAME, devName.getText().toString());
			recStore.addData(DroidGuideConstants.UPDATE_INTERVAL, updtInterval.getText().toString());
			recStore.addData(DroidGuideConstants.NOTIFICATION_LEVEL, notLevel.getSelectedItem().toString());
			
			AndroidUtils.showAlert(this, "Configuration saved successfully.");
			finish();
		}
		
	}
	
//    protected Dialog onCreateDialog(int id) {
//        switch (id) {
//        	case 0:
//                return new AlertDialog.Builder(this)
//                .setTitle("Error")
//                .setItems(R.array.search_menu, new DialogClick())
//                .create();
//           
//        }
//		return new Dialog(this);
//     }
//    private class DialogClick implements DialogInterface.OnClickListener {
//        public void onClick(DialogInterface dialog, int which) {
//        	String url = DGRecordStore.getInstance().getData("server");
//
//        	switch (which) {
//            	case 0:
//            		wv.loadUrl(url+"map/all?userid=agpkcm9pZGd1aWRlchULEg5Ecm9pZEd1aWRlVXNlchjeBAw&panoramio=true");
//            		break;
//            	case 1:
//            		wv.loadUrl(url+"map/all?userid=agpkcm9pZGd1aWRlchULEg5Ecm9pZEd1aWRlVXNlchjeBAw&wikipedia=true");
//        	}
//        }
//    }
	
}
